package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/reg")
    public String reg(@RequestBody User user){
        return userService.reg(user);
    }

    @RequestMapping("/login")
    public User login(@RequestBody User user){
        User u=userService.login(user);
        if(u==null){
            return null;
        }
        return u;
    }

    @RequestMapping("/list")
    public SysResult findUserListByPage(@RequestBody PageResult pageResult){
        pageResult =userService.findUserListByPage(pageResult);
        return SysResult.success(pageResult);
    }

    @RequestMapping("/status")
    public SysResult updateStatus(@RequestBody User user){
        userService.updateStatus(user);
        return SysResult.success();
    }

    @RequestMapping( "/updateUserAdmin")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }

    @RequestMapping("/id")
    public SysResult findUserById(@RequestBody User user){
        return SysResult.success(userService.findUserById(user.getId()));
    }

    @RequestMapping( "/dlid")
    public SysResult deleteUserById(@RequestBody User user){
        userService.deleteUserById(user.getId());
        return SysResult.success();
    }

    @RequestMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }
}
